package com.prs.crm.action.oa;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.oa.OaTaskType;
import com.prs.crm.service.oa.OaTaskTypeService;
import com.prs.crm.service.sys.LogService;

public class OaTaskTypeAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private OaTaskType oaTaskType;
	private OaTaskTypeService oaTaskTypeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private Collection<TreeNode> nodes;
	private LogService logService;

	
	@Action("listTaskType")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(OaTaskType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getOaTaskTypeService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editTaskType")
	public String edit() {
		return SUCCESS;
	}
	@Action(value="loadTaskTypeTree",results={@Result(name="success",type="json",params={"root","nodes"})})
	public String loadTaskTypeTree(){
		nodes = new ArrayList<TreeNode>();
		List<OaTaskType> types = this.getOaTaskTypeService().getAllTaskType();
		for(OaTaskType type : types) {
			TreeNode node =new TreeNode();
			node.setId(type.getId().toString());
			node.setText(type.getName());
			nodes.add(node);
		}
		return SUCCESS;
	}
	
	@Action(value="saveTaskType",results={@Result(name="success",type="redirect",location="listTaskType"),
			@Result(name="error",type="chain",location="editTaskType")})
	public String save() {
		if(oaTaskType.getName()==null||"".equals(oaTaskType.getName().trim())){
			addActionError("类型不能为空");
			return ERROR;
		}if (this.getLogService().getSearchName(OaTaskType.class, oaTaskType
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		
		this.getOaTaskTypeService().save(oaTaskType);
		return SUCCESS;
	}
	
	@Action(value="removeTaskType",results={@Result(name="success",type="redirect",location="listTaskType"),
			@Result(name="error",type="chain",location="listTaskType")})
	public String remove() {		
		if(ids == null) {
			this.addActionError("请选择要删除的工作类型");
			return ERROR;
		}
		for(Integer id : ids) {
			OaTaskType type = this.getOaTaskTypeService().get(id);
			if(type.getOaTasks().size() != 0) {
				this.addActionError("类型已引用，不能删除");
				return ERROR;
			}
		}
		this.getOaTaskTypeService().batchRemove(ids);
		return SUCCESS;
	}
	
	public OaTaskType getOaTaskType() {
		return oaTaskType;
	}
	public void setOaTaskType(OaTaskType oaTaskType) {
		this.oaTaskType = oaTaskType;
	}
	public OaTaskTypeService getOaTaskTypeService() {
		return oaTaskTypeService;
	}
	public void setOaTaskTypeService(OaTaskTypeService oaTaskTypeService) {
		this.oaTaskTypeService = oaTaskTypeService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}
	
	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
